package com.tutu.product.controller;


import com.tutu.commons.pojo.Picture;
import com.tutu.commons.pojo.Product;
import com.tutu.commons.utils.CollectError;
import com.tutu.commons.utils.R;
import com.tutu.product.checks.GetProductByCategoryParam;
import com.tutu.product.service.PictureService;
import com.tutu.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Program: b2c-cloud-store
 * @Description:
 * @Author: JK.TuTu
 * @Version: 1.0
 * @Create: 2022-12-15 10:33
 **/
@Slf4j
@RestController
@RequestMapping("/product")
@Api("商品相关api")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private PictureService pictureService;


    /**
     * 根据商品类别(单个或多个)名称 获取该类别下的所有商品信息
     *
     * @param categoryName 商品类别名称集合
     * @return 返回结果实体对象
     */
    @Cacheable(value = "list.product", key = "#categoryName")
    @ApiOperation("根据商品类别名称(单个或多个) 获取该类别下的所有商品信息")
    @GetMapping("/hots")
    public R hots(@RequestParam @Validated List<String> categoryName) {
        return productService.getProductInfo(categoryName);
    }

    /**
     * 通过商品Id获取商品信息
     *
     * @param productId 商品Id
     * @return 返回结果实体对象
     */
    @Cacheable(value = "product", key = "#productId")
    @ApiOperation(" 通过商品Id获取商品信息")
    @GetMapping("/detail")
    public R getProductById(@RequestParam(value = "productId", required = true) Integer productId) {
        Product product = productService.getProductInfoById(productId);
        return R.ok(product);
    }


    /**
     * 根据商品分类ID获取商品的数据
     *
     * @param param  校验参数实体类
     * @param result 检验结果集
     * @return 返回结果实体类
     */
    @Cacheable(value = "list.product", key = "#param.categoryId+" + "'-'+#param.currentPage+" + "'-'+ #param.pageSize")
    @ApiOperation("根据商品类别ID获取商品的数据")
    @PostMapping("/all")
    public R getProductsByCategoryId(@RequestBody @Validated GetProductByCategoryParam param, BindingResult result) {
        if (result.hasErrors()) {
            String errorInfo = CollectError.getErrorInfo(result);
            return R.fail(errorInfo);
        }
        return productService.getProductsByCategoryId(param);

    }

    /**
     * 通过商品Id获取商品图片信息
     *
     * @param productId 商品Id
     * @return 返回结果实体对象
     */
    @Cacheable(value = "product_pictures", key = "#productId")
    @ApiOperation("根据商品类别ID获取商品图片信息")
    @GetMapping("/pictures")
    public R getProductPicturesById(@RequestParam(value = "productId", required = true) Integer productId) {
        List<Picture> pictures = pictureService.getProductPicturesById(productId);
        return R.ok(pictures);
    }

    /**
     * 获取所有商品分类
     *
     * @return 返回检验结果集
     */
    @ApiOperation("获取所有商品分类")
    @GetMapping("/category/list")
    public R getAllCategories() {
        return productService.getAllCategories();
    }

    /**
     * 支持模糊查询商品信息
     *
     * @param param  商品查询的参数检验实体类
     * @param result 参数检验实体类
     * @return 返回结果实体类
     */
    @Cacheable(value = "list.product", key = "#param.categoryId+" + "'-'+#param.currentPage+" + "'-'+ #param.pageSize")
    @ApiOperation("支持模糊查询商品信息")
    @PostMapping("/search")
    public R searchProduct(@RequestBody @Validated GetProductByCategoryParam param, BindingResult result) {
        if (result.hasErrors()) {
            String errorInfo = CollectError.getErrorInfo(result);
            return R.fail(errorInfo);
        }
        return productService.searchProduct(param);

    }


}
